package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.dto.CheckMarkDtoDto;
import com.yunyao.common.dto.PrePaymentDto;
import com.yunyao.common.web.util.ConvertUtil;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.dao.model.BamuPoPrePayment;
import com.yunyao.dao.mapper.BamuPoPrePaymentMapper;
import com.yunyao.dao.service.bamu.IBamuPoPrePaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * <p>
 * 供货订单预付款表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuPoPrePaymentServiceImpl extends ServiceImpl<BamuPoPrePaymentMapper, BamuPoPrePayment> implements IBamuPoPrePaymentService {


    @Override
    public PageResult<BamuPoPrePayment> pageList(@Valid @NotNull PrePaymentDto dto) {
        LambdaQueryWrapper<BamuPoPrePayment> wrapper = new LambdaQueryWrapper<>();
        //.eq(BamuPoPrePayment::getContractType, dto.getContractType())
        wrapper.eq(dto.getStatus() != null, BamuPoPrePayment::getStatus, dto.getStatus())
                .like(StringUtils.isNotBlank(dto.getContractName()), BamuPoPrePayment::getContractName, dto.getContractName())
                .like(StringUtils.isNotBlank(dto.getProjectName()), BamuPoPrePayment::getProjectName, dto.getProjectName())
                .between(StringUtils.isNotBlank(dto.getBeginDate()) && StringUtils.isNotBlank(dto.getEndDate()), BamuPoPrePayment::getContractPrePaymentDate,
                        ConvertUtil.getStartOfDay(ConvertUtil.getString2Date(dto.getBeginDate())), ConvertUtil.getEndOfDay(ConvertUtil.getString2Date(dto.getEndDate())))
                .eq(BamuPoPrePayment::getIsDeleted, 0)
                .orderByDesc(BamuPoPrePayment::getCreatedAt);
        IPage<BamuPoPrePayment> contractPage = new Page<>(dto.getPageIndex(), dto.getPageSize());
        contractPage = page(contractPage, wrapper);
        if (CollectionUtils.isEmpty(contractPage.getRecords()))
            return new PageResult<>();
        return new PageResult<>(contractPage.getRecords(), contractPage.getCurrent(), contractPage.getSize(), contractPage.getPages(), contractPage.getTotal());
    }

    @Override
    public BamuPoPrePayment findByIdInfo(@NotBlank String id) {
        return lambdaQuery()
                .eq(BamuPoPrePayment::getId, id)
                .eq(BamuPoPrePayment::getIsDeleted, 0)
                .one();
    }

    @Override
    public void add(BamuPoPrePayment dto) {
        Date date = new Date();
        dto.setIsDeleted(0);
        dto.setCreatedAt(date);
        dto.setUpdatedAt(date);
        save(dto);
    }

    @Override
    public void finishPayment(@Valid @NotNull CheckMarkDtoDto dto) {
        lambdaUpdate()
                .eq(BamuPoPrePayment::getId, dto.getPaymentId())
                .eq(BamuPoPrePayment::getContractType, dto.getContractType())
                .set(BamuPoPrePayment::getStatus, 9)
                .set(StringUtils.isNotBlank(dto.getCheckMark()), BamuPoPrePayment::getCheckMark, dto.getCheckMark())
                .update();
    }

}
